﻿using System;
using System.Globalization;
using System.Windows.Data;
namespace CoreModule.Converters
{
    /// <summary>
    ///字符省略
    /// </summary>
    public class StringEllipsisConverter : IValueConverter
    {
        /// <summary>
        /// 
        /// </summary>
        public int Capacity { get; set; } = 18;
        /// <summary>
        /// 
        /// </summary>
        public string Suffix { get; set; } = "...";
        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
            {
                return string.Empty;
            }
            if (value is string)
            {
                var str=value as string;
                if (str.Length<=Capacity)
                {
                    return str;
                }
                else
                {
                    return str.Substring(0, Capacity - Suffix.Length) + Suffix;
                }
            }
            return string.Empty;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
